﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TrifleCommon;
using TestTrifleHostsConsole.com.sdo.zhoucong;

namespace TestTrifleHostsConsole
{
    class Program
    {
        static void Main(string[] args)
        {
            c.DelayedByeByeCompleted += WindowsHostDelayedByeByeCompleted;
            trifleService.DelayedByeByeCompleted += TestSingleCallExceptionCompleted;
            //TestTrifleWCFWebHost();
            //TestTrifleWCFWindowsService();
            //TestBug();
            //TestAsyncTrifleWCFWindowsService();
            TestSingleCallException();
        }

        #region ---- 单调用异常 ----

        static void TestSingleCallExceptionCompleted(object sender, DelayedByeByeCompletedEventArgs e)
        {
            try
            {
                Logger.Info("TestSingleCallExceptionCompleted");
                Console.WriteLine(e.Result);
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
                Logger.Exception("TestSingleCallExceptionCompleted", ex);
            }
        }

        static Trifle trifleService = new Trifle();

        static void TestSingleCallException()
        {
            Console.WriteLine("==============================\n" +
                              "Trifle Web Service Test Async Single Call Exception");
            string s = "";
            s = Console.ReadLine();
            while (s.ToString() != "")
            {
                Console.WriteLine("Read:->" + s + "<-");
                
                {
                    
                    trifleService.DelayedByeByeAsync(int.Parse(s),new object());
                }
                s = Console.ReadLine();
            }
        }

        #endregion ---- 单调用异常 ----


        #region ---- 以Windows服务为宿主的WCF服务方法异步调用的测试 ----

        static void WindowsHostDelayedByeByeCompleted(
            object sender, TrifleWindowsService.DelayedByeByeCompletedEventArgs e)
        {
            try
            {
                Logger.Info("WindowsHostDelayedByeByeCompleted");
                Console.WriteLine(e.Result);
            }
            catch (Exception ex)
            {
                Console.Write(ex.ToString());
                Logger.Exception("WindowsHostDelayedByeByeCompleted", ex);
            }
        }
        static TrifleWindowsService.TrifleClient c = new TrifleWindowsService.TrifleClient();
        static void TestAsyncTrifleWCFWindowsService()
        {
            Console.WriteLine("==============================\n" +
                              "Trifle Windows Service Test Async");
            string s = "";
            s = Console.ReadLine();
            while (s.ToString() != "")
            {
                Console.WriteLine("Read:->" + s + "<-");
                {
                    c.DelayedByeByeAsync(int.Parse(s));
                }
                s = Console.ReadLine();
            }
        }

        #endregion ---- 以Windows服务为宿主的WCF服务方法调用的测试 ----


        #region ---- 测试导致Windows服务挂掉的原因是否是：WCF服务的异常没有捕捉到 ----

        static void TestBug()
        {
            TestTrifleHostsConsole.TrifleWindowsService.TrifleClient c =
                new TestTrifleHostsConsole.TrifleWindowsService.TrifleClient();
            c.TestBug();
        }

        #endregion


        #region ---- 以Windows服务为宿主的WCF服务方法同步调用的测试 ----

        static void TestTrifleWCFWindowsService()
        {
            Console.WriteLine("==============================\n" +
                              "Trifle Windows Service Test");
            string s = "";
            s = Console.ReadLine();
            while (s.ToString() != "")
            {
                Console.WriteLine("Read:->" + s + "<-");
                using (TrifleWindowsService.TrifleClient c = new TrifleWindowsService.TrifleClient())
                {
                    Console.WriteLine(c.DelayedByeBye(int.Parse(s)));
                }
                s = Console.ReadLine();
            }
        }

        #endregion ---- 以Windows服务为宿主的WCF服务方法同步调用的测试 ----


        #region ---- 以IIS为宿主的WCF服务方法同步调用的测试 ----

        static void TestTrifleWCFWebHost()
        {
            Console.WriteLine("==============================\n" +
                              "Trifle Web Host Service Test");
            string s = "";
            s = Console.ReadLine();
            while (s.ToString() != "")
            {
                Console.WriteLine("Read:->" + s + "<-");
                using (TestTrifleHostsConsole.TrifleWebHostService.TrifleClient c = 
                    new TestTrifleHostsConsole.TrifleWebHostService.TrifleClient())
                {
                    Console.WriteLine(c.DelayedByeBye(int.Parse(s)));
                }
                s = Console.ReadLine();
            }
        }

        #endregion ---- 以IIS为宿主的WCF服务方法同步调用的测试 ----
    }
}
